package com.example.webcrawler.service.mpl;

import com.example.webcrawler.model.ApiRequest;
import com.example.webcrawler.model.FlowTest;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.UUID;

/**
 * 数据存储服务，用于替代静态列表存储
 */
@Service
public class DataStorageService {

    // 使用线程安全的集合来存储数据
    private final List<ApiRequest> savedRequests = new CopyOnWriteArrayList<>();
    private final List<FlowTest> savedFlows = new CopyOnWriteArrayList<>();

    /**
     * 保存请求
     */
    public ApiRequest saveRequest(ApiRequest request) {
        // 为请求分配唯一ID
        if (request.getRequestId() == null || request.getRequestId().isEmpty()) {
            request.setRequestId(UUID.randomUUID().toString());
        }
        savedRequests.add(request);
        return request;
    }

    /**
     * 获取所有已保存的请求
     */
    public List<ApiRequest> getAllRequests() {
        return new ArrayList<>(savedRequests);
    }

    /**
     * 根据请求ID更新请求
     */
    public boolean updateRequest(String requestId, ApiRequest request) {
        for (int i = 0; i < savedRequests.size(); i++) {
            if (requestId.equals(savedRequests.get(i).getRequestId())) {
                request.setRequestId(requestId); // 确保ID不变
                savedRequests.set(i, request);
                return true;
            }
        }
        return false;
    }

    /**
     * 根据请求ID删除请求
     */
    public boolean deleteRequest(String requestId) {
        for (int i = 0; i < savedRequests.size(); i++) {
            if (requestId.equals(savedRequests.get(i).getRequestId())) {
                savedRequests.remove(i);
                return true;
            }
        }
        return false;
    }

    /**
     * 根据流程ID获取请求
     */
    public List<ApiRequest> getRequestsByFlowId(String flowId) {
        List<ApiRequest> result = new ArrayList<>();
        for (ApiRequest request : savedRequests) {
            if (flowId.equals(request.getFlowId())) {
                result.add(request);
            }
        }
        return result;
    }

    /**
     * 保存流程测试
     */
    public FlowTest saveFlow(FlowTest flowTest) {
        // 为流程分配唯一ID
        if (flowTest.getId() == null || flowTest.getId().isEmpty()) {
            flowTest.setId(UUID.randomUUID().toString());
        }

        // 检查是否已存在同名流程
        int existingIndex = -1;
        for (int i = 0; i < savedFlows.size(); i++) {
            if (savedFlows.get(i).getId().equals(flowTest.getId())) {
                existingIndex = i;
                break;
            }
        }

        if (existingIndex >= 0) {
            savedFlows.set(existingIndex, flowTest);
        } else {
            savedFlows.add(flowTest);
        }

        return flowTest;
    }

    /**
     * 获取所有已保存的流程
     */
    public List<FlowTest> getAllFlows() {
        return new ArrayList<>(savedFlows);
    }

    /**
     * 根据流程ID删除流程
     */
    public boolean deleteFlow(String flowId) {
        for (int i = 0; i < savedFlows.size(); i++) {
            if (flowId.equals(savedFlows.get(i).getId())) {
                savedFlows.remove(i);
                return true;
            }
        }
        return false;
    }
}
